/*
 * Copyright (c) 2020 Huawei Technologies Co.,Ltd.
 *
 * openGauss is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *
 *    http://license.coscl.org.cn/MulanPSL2
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 * ---------------------------------------------------------------------------------------
 * 
 * gs_imcs.h
 *    Definition about catalog of imcs.
 * 
 * 
 * IDENTIFICATION
 *    src/include/catalog/gs_imcs.h
 *
 * ---------------------------------------------------------------------------------------
 */

#ifndef GS_IMCS_H
#define GS_IMCS_H

#include "catalog/genbki.h"
#include "nodes/parsenodes.h"
#include "utils/date.h"
#include <unordered_map>
#include "access/imcs/imcu_cache_mgr.h"

#define ImcsRelationId 8881
#define ImcsRelationId_Rowtype_Id 8882

#define HTAP_HAS_IMCS (pg_atomic_read_u32(&(IMCU_CACHE->m_imcs_cnt)) != 0)

CATALOG(gs_imcs,8881) BKI_SCHEMA_MACRO
{
    Oid reloid;
    NameData relname;
    Oid imcsoid;
    NameData imcsmetaname;
    Oid parentoid;
    bool imcsispart;
    int2 imcsnattr;
    int2vector imcskey;
} FormData_gs_imcs;

typedef FormData_gs_imcs *Form_gs_imcs;

#define Natts_gs_imcs 9

#define Anum_gs_imcs_reloid 1
#define Anum_gs_imcs_relname 2
#define Anum_gs_imcs_imcsoid 3
#define Anum_gs_imcs_imcsmetaname 4
#define Anum_gs_imcs_imcsparentid 5
#define Anum_gs_imcs_imcsispart 6
#define Anum_gs_imcs_imcsnattr 7
#define Anum_gs_imcs_imcskey 8

#define Natts_gs_imcu_descs 10

#define Anum_gs_imcu_reloid 1
#define Anum_gs_imcu_xmin 2
#define Anum_gs_imcu_id 3
#define Anum_gs_imcu_min 4
#define Anum_gs_imcu_max 5
#define Anum_gs_imcu_row_count 6
#define Anum_gs_imcu_size 7
#define Anum_gs_imcu_mode 8
#define Anum_gs_imcu_pointer 9
#define Anum_gs_imcu_magic 10

#define Natts_gs_imcs_mem_status 10

#define Anum_imcs_desc_reloid 1
#define Anum_gs_imcs_desc_rowgroup_num 2
#define Anum_gs_imcs_desc_mem_num 3
#define Anum_gs_imcs_desc_mem_size 4
#define Anum_gs_imcs_desc_disk_num 5
#define Anum_gs_imcs_desc_disk_size 6
#define Anum_gs_imcs_desc_last_flushed_csn 7
#define Anum_gs_imcs_desc_delta_row_count 8
#define Anum_gs_imcs_desc_delta_mem_size 9
#define Anum_gs_imcs_delta_visible_rows 10

#define Natts_gs_imcu_slot_status 6

#define Anum_imcu_slot_reloid 1
#define Anum_gs_imcu_slot_cu_id 2
#define Anum_gs_imcu_slot_col_id 3
#define Anum_gs_imcu_slot_id 4
#define Anum_gs_imcu_slot_datablock_size 5
#define Anum_gs_imcu_slot_flag 6

#define Natts_sys_view 9

#define Anum_sys_view_reloid 1
#define Anum_sys_view_relname 2
#define Anum_sys_view_imcsoid 3
#define Anum_sys_view_imcsmetaname 4
#define Anum_sys_view_imcsparentid 5
#define Anum_sys_view_imcsispart 6
#define Anum_sys_view_imcsstatus 7
#define Anum_sys_view_imcsnattr 8
#define Anum_sys_view_imcskey 9

#define Nnum_delta_fields 3

#define Anum_delta_csn 1
#define Anum_delta_rowid 2
#define Anum_delta_status 3

extern Datum gs_imcs_views(PG_FUNCTION_ARGS);
#ifdef ENABLE_HTAP
extern void gs_imcs_insert_tuple(Oid rel_oid, char* rel_name, Oid imcs_oid, char* imcs_meta_name,
                                         bool is_partition, Oid parentid, int2 nattr, int2vector* imcskey);
extern void gs_imcs_delete_tuple(Oid imcs_oid, std::unordered_map<Oid, RelFileNode>* oid_del_info);
extern Oid search_table_from_gs_imcs(Oid rel_oid);
extern Form_gs_imcs search_records_from_gs_imcs(Oid rel_oid);
extern Oid search_rel_oid_from_hashtable(Oid rel_oid);
extern bool search_parent_oid_from_gs_imcs_for_partition(Oid parent_oid, Oid* imcs_oid);
extern bool search_parent_oid_from_gs_imcs_for_subpartition(Oid parent_oid, Oid* imcs_oid);
extern void delete_gs_imcs_table_on_rel(Relation rel, std::unordered_map<Oid, RelFileNode>* oid_del_info);
#endif
#endif /* GS_IMCS_H */